package com.bdqn.t382.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.t382.entity.Brand;
import com.bdqn.t382.entity.VehicleComplaint;
import com.bdqn.t382.entity.vehiclecomplaintsAndBrand;
import com.bdqn.t382.mapper.BrandMapper;
import com.bdqn.t382.mapper.VehicleComplaintsMapper;
import com.bdqn.t382.service.CarService;
import com.bdqn.t382.util.RedisUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    private BrandMapper brandMapper;

    @Resource
    private VehicleComplaintsMapper vehicleComplaintsMapper;

    @Resource
    private RedisUtil redisUtil;

    // 用于对象与 JSON 之间的序列化和反序列化操作
    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 实现获取所有品牌的方法
    @Override
    public List<Brand> getAllBrands() {
        String key = "allBrands";
        String cachedBrandsJson = redisUtil.getKey(key);
        if (cachedBrandsJson != null) {
            try {
                // 将 JSON 字符串反序列化为 List<Brand>
                return objectMapper.readValue(cachedBrandsJson, new TypeReference<List<Brand>>() {});
            } catch (JsonProcessingException e) {
                // 处理反序列化异常
                System.err.println("Failed to deserialize cached brands: " + e.getMessage());
            }
        }
        // 若缓存中没有数据，从数据库获取
        List<Brand> brands = brandMapper.selectList(null);
        try {
            // 将 List<Brand> 序列化为 JSON 字符串
            String brandsJson = objectMapper.writeValueAsString(brands);
            // 存储到 Redis 并设置过期时间
            redisUtil.setKey(key, brandsJson, 3600);
        } catch (JsonProcessingException e) {
            // 处理序列化异常
            System.err.println("Failed to serialize brands for caching: " + e.getMessage());
        }
        return brands;
    }

// 实现根据品牌获取车辆投诉的方法
    @Override
    public List<vehiclecomplaintsAndBrand> getComplaintsByBrandName(String brandName) {
        String key = "complaintsByBrand:" + brandName;
        String cachedComplaintsJson = redisUtil.getKey(key);
        if (cachedComplaintsJson != null) {
            try {
                // 将 JSON 字符串反序列化为 List<VehicleComplaintsAndBrand>
                return objectMapper.readValue(cachedComplaintsJson, new TypeReference<List<vehiclecomplaintsAndBrand>>() {});
            } catch (IOException e) {
                System.err.println("序列化失败 " + e.getMessage());
            }
        }
        // 若缓存中没有数据，从数据库获取
        List<vehiclecomplaintsAndBrand> complaints = vehicleComplaintsMapper.getComplaintsByBrand(brandName);
        try {
            // 将 List<VehicleComplaintsAndBrand> 序列化为 JSON 字符串
            String complaintsJson = objectMapper.writeValueAsString(complaints);
            // 存储到 Redis 并设置过期时间
            redisUtil.setKey(key, complaintsJson, 3600);
        } catch (IOException e) {
            System.err.println("序列化失败： " + e.getMessage());
        }
        return complaints;
    }

    @Override
    public int addComplaint(vehiclecomplaintsAndBrand complaint) {
       int count = vehicleComplaintsMapper.insert(complaint);
       return count;
    }

//
//    @Override
//    public void updateComplaintStatus(VehicleComplaint complaint) {
//        vehicleComplaintsMapper.updateById(complaint);
//    }
}